package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.family.admin.service.*;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.BaseServiceImpl;
import com.core.common.gen.PageQuery;
import com.core.family.admin.domain.dto.SysUserDto;
import com.core.family.admin.domain.po.SysRoleG;
import com.core.family.admin.domain.po.SysRoleMenu;
import com.core.family.admin.domain.po.SysUser;
import com.core.family.admin.domain.po.SysUserGroup;
import com.core.family.admin.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    SysGroupService groupService;
    @Autowired
    SysMenuService menuService;
    @Autowired
    SysRoleGService roleGService;
    @Autowired
    SysRoleMenuService roleMenuService;

    @Autowired
    SysUserGroupService userGroupService;
    @Autowired
    SysRoleService roleService;


    @Override
    public SysUser getUser(SysUser user) {
        return getOne(Wrappers.lambdaQuery(user));
    }

    @Override
    public SysUserDto getUserInfo(Serializable id) {
        SysUser user = getById(id);
        SysUserDto userDto = new SysUserDto();
        BeanUtils.copyProperties(user, userDto);

        List<SysUserGroup> userGroupList = userGroupService.getByUserId(user.getOrgId(), id);
        List<Integer> groupIds = userGroupList.stream().map(SysUserGroup::getGid).collect(Collectors.toList());

        if (org.springframework.util.CollectionUtils.isEmpty(groupIds)) {
            return userDto;
        }
        userDto.setGroups(groupService.listByIds(groupIds));

        List<SysRoleG> roleGList = roleGService.listByGroupIds(user.getOrgId(), groupIds);
        List<Integer> roleIds = roleGList.stream().map(SysRoleG::getRid).collect(Collectors.toList());
        if (org.springframework.util.CollectionUtils.isEmpty(roleIds)) {
            return userDto;
        }
        userDto.setRoles(roleService.listByIds(roleIds));

        List<SysRoleMenu> roleMenuList = roleMenuService.getByRoleIds(user.getOrgId(), roleIds);
        List<Integer> menuIds = roleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        if (org.springframework.util.CollectionUtils.isEmpty(menuIds)) {
            return userDto;
        }

        userDto.setMenus(menuService.listByIds(menuIds));

        return userDto;

    }


    @Override
    public IPage selectPage(SysUser sysUser, PageQuery<BaseQuery> pageQuery) {
        IPage<SysUser> page = super.selectPage(sysUser, pageQuery);
        List<SysUserDto> list = page.getRecords().stream()
                .map(bean -> {
                    return getUserInfo(bean.getId());
                }).collect(Collectors.toList());
        IPage iPage = new Page();
        BeanUtils.copyProperties(page, iPage);
        iPage.setRecords(list);
        return iPage;
    }
}

